<?php 
class docsDAL 
{
	public function Insert($db, $docsObj, &$errorMsg) 
	{
		$errorMsg = "";

		$query = "INSERT INTO docs (name, originalName, type, size, uploadedTime, uploadedBy, uploadedDir) VALUES (".ToSQLPostVAL($docsObj->name).", ".ToSQLPostVAL($docsObj->originalName).", ".ToSQLPostVAL($docsObj->type).", ".ToSQLPostVAL($docsObj->size).", ".ToSQLPostVAL($docsObj->uploadedTime).", ".ToSQLPostVAL($docsObj->uploadedBy).", ".ToSQLPostVAL($docsObj->uploadedDir).")";
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}

		$docsObj->id = mysql_insert_id();
		return true;
	}

	public function Update($db, $docsObj, &$errorMsg)
	 {	
		$errorMsg = "";

		$query = "UPDATE docs SET name=".ToSQLPostVAL($docsObj->name).", originalName=".ToSQLPostVAL($docsObj->originalName).", type=".ToSQLPostVAL($docsObj->type).", size=".ToSQLPostVAL($docsObj->size).", uploadedTime=".ToSQLPostVAL($docsObj->uploadedTime).", uploadedBy=".ToSQLPostVAL($docsObj->uploadedBy).", uploadedDir=".ToSQLPostVAL($docsObj->uploadedDir)." WHERE id=".ToSQLPostVAL($docsObj->id);
		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}

		return true;
	}

	public function Load($db, $id, &$errorMsg, $joinTables="", $onFields="", $getFields="")
	{
		$errorMsg = "";
		$joinClause = "";
		$extraFields = "";
		for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
		{
			$joinClause .= " LEFT JOIN ".$joinTables[$i]." ".$joinTables[$i]." ON ".$onFields[$i];
			$fields = explode(",", $getFields[$i]);
			for ($j=0; $j<count($fields); $j++)
				$extraFields .= ", ".$joinTables[$i].".".$fields[$j]." AS ".$joinTables[$i]."_".$fields[$j];
		}
		$query	= "SELECT docs.*$extraFields FROM docs $joinClause WHERE docs.id=".ToSQLPostVAL($id);

		if (!$db->query($query)) {
			$errorMsg	= mysql_error()." Query: $query.";
			return null;
		} else {
			if ($db->next_record()) {
				$docsObj = new docsObj($db->f("id"), $db->f("name"), $db->f("originalName"), $db->f("type"), $db->f("size"), $db->f("uploadedTime"), $db->f("uploadedBy"), $db->f("uploadedDir"));
				for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
				{
					$fields = explode(",", $getFields[$i]);
					for ($j=0; $j<count($fields); $j++)
					{
						$docsObj->otherFields[$joinTables[$i]."_".$fields[$j]] = $db->f($joinTables[$i]."_".$fields[$j]);
					}
				}
				return $docsObj;
			} else {
				$errorMsg = "Data not found.";
				return null;
			}
		}
		return null;
	}

	public function IsExist($db, $whereStr) 
	{
		$query	= "SELECT * FROM docs";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		else return false;

		if (!$db->query($query)) {
			return false;
		} else {
			while ($db->next_record()) {
				return true;
			}
		}
		return false;
	}

	public function Pure($db, $whereStr, &$errorMsg) 
	{
		$query	= "DELETE FROM docs";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		else {
			$errorMsg = "You have no delete condition in query string ($query WHERE ?).";
			return false;
		}

		if (!$db->query($query)) {
			$errorMsg = mysql_error()." Query: $query.";
			return false;
		}
		return true;
	}

	public function GetList($db, $whereStr, $orderByStr, $limit, &$errorMsg, $joinTables="", $onFields="", $getFields="") 
	{
		$returnData = array();
		$joinClause = "";
		$extraFields = "";
		for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
		{
			$joinClause .= " LEFT JOIN ".$joinTables[$i]." ".$joinTables[$i]." ON ".$onFields[$i];
			$fields = explode(",", $getFields[$i]);
			for ($j=0; $j<count($fields); $j++)
				$extraFields .= ", ".$joinTables[$i].".".$fields[$j]." AS ".$joinTables[$i]."_".$fields[$j];
		}
		$errorMsg = "";
		$query	= "SELECT docs.*$extraFields FROM docs$joinClause";

		if ($whereStr!="") $query = $query." WHERE ".$whereStr;
		if ($orderByStr!="") $query = $query." ORDER BY ".$orderByStr;
		if ($limit!="") $query = $query." LIMIT ".$limit;

		if (!$db->query($query)) {
			$errorMsg	= mysql_error()." Query: $query.";
		} else {
			while ($db->next_record()) 	{
				$objVal = new docsObj($db->f("id"), $db->f("name"), $db->f("originalName"), $db->f("type"), $db->f("size"), $db->f("uploadedTime"), $db->f("uploadedBy"), $db->f("uploadedDir"));
				for ($i=0; is_array($joinTables) && $i<count($joinTables); $i++)
				{
					$fields = explode(",", $getFields[$i]);
					for ($j=0; $j<count($fields); $j++)
					{
						$objVal->otherFields[$joinTables[$i]."_".$fields[$j]] = $db->f($joinTables[$i]."_".$fields[$j]);
					}
				}
				$returnData[] = $objVal;
			}
		}
		return $returnData;
	}

	public function Push($docsObj)
	{
		global $id, $name, $originalName, $type, $size, $uploadedTime, $uploadedBy, $uploadedDir;

		$id = $docsObj->id;
		$name = $docsObj->name;
		$originalName = $docsObj->originalName;
		$type = $docsObj->type;
		$size = $docsObj->size;
		$uploadedTime = $docsObj->uploadedTime;
		$uploadedBy = $docsObj->uploadedBy;
		$uploadedDir = $docsObj->uploadedDir;
	}

	public function Pop(&$docsObj)
	{
		global $id, $name, $originalName, $type, $size, $uploadedTime, $uploadedBy, $uploadedDir;

		if (isset($id)) $docsObj->id = $id;
		if (isset($name)) $docsObj->name = $name;
		if (isset($originalName)) $docsObj->originalName = $originalName;
		if (isset($type)) $docsObj->type = $type;
		if (isset($size)) $docsObj->size = $size;
		if (isset($uploadedTime)) $docsObj->uploadedTime = $uploadedTime;
		if (isset($uploadedBy)) $docsObj->uploadedBy = $uploadedBy;
		if (isset($uploadedDir)) $docsObj->uploadedDir = $uploadedDir;
	}

} 
?>